package best.driver.android.View;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import best.driver.android.R;
import best.driver.android.Utils.SizeUtils;

public abstract class BestAlertDialogBase extends Dialog implements View.OnClickListener {

    public static final int BUTTON_CLOSE = 0;
    public static final int BUTTON_OK = 1;
    public static final int BUTTON_CANCEL = 2;

    public static interface BestAlertDialogOnClickListener {
        public boolean onClick(BestAlertDialogBase dialog, int button);
    }

    protected ImageView btnClose;
    protected Button btnCancel;
    protected Button btnOk;

    protected TextView tvTitle;
    protected View viewContent;
    private View viewSpace;

    protected BestAlertDialogOnClickListener onClickListener;

    public BestAlertDialogBase(Context context, int layoutId, BestAlertDialogOnClickListener onClickListener) {
        super(context, R.style.alert_dialog);
        initView(layoutId);
        this.onClickListener = onClickListener;
        setCanceledOnTouchOutside(false);
    }

    private void initView(int layoutId) {
        View rootView = LayoutInflater.from(getContext()).inflate(layoutId, null);
        btnClose = (ImageView)rootView.findViewById(R.id.btn_close);
        btnCancel = (Button)rootView.findViewById(R.id.btn_cancel);
        btnOk = (Button)rootView.findViewById(R.id.btn_ok);

        tvTitle = (TextView)rootView.findViewById(R.id.tv_title);
        viewContent = rootView.findViewById(R.id.view_content);
        viewSpace = rootView.findViewById(R.id.view_space);

        btnClose.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        btnOk.setOnClickListener(this);

        setContentView(rootView);
    }

    protected void setTitle(String title) {
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
    }

    public void setButtonText(String cancel, String ok) {
        if (TextUtils.isEmpty(ok)) {
            ok = "确定";
        }
        if (TextUtils.isEmpty(cancel)) {
            cancel = "取消";
        }
        btnOk.setText(ok);
        btnCancel.setText(cancel);
    }

    @Override
    public void setCancelable(boolean flag) {
        super.setCancelable(flag);
        btnClose.setVisibility(flag ? View.VISIBLE : View.INVISIBLE);
    }

    public void setCancelButtonVisible(int visibility) {
        btnCancel.setVisibility(visibility);
        viewSpace.setVisibility(visibility);
        int okBtnMargin = 0;
        if (visibility != View.VISIBLE) {
            okBtnMargin = SizeUtils.dp2px(getContext(), 17);
        }
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)btnOk.getLayoutParams();
        params.leftMargin = params.rightMargin = okBtnMargin;
        btnOk.setLayoutParams(params);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                cancel();
                break;
            case R.id.btn_cancel:
                click(BUTTON_CANCEL);
                break;
            case R.id.btn_ok:
                click(BUTTON_OK);
                break;
        }
    }

    private void click(int button) {
        if (onClickListener != null) {
            if (onClickListener.onClick(this, button)) {
                return;
            }
        }
        dismiss();
    }
}
